//
//  UIViewController+Category.m
//  baby
//
//  Created by wenju on 14-6-25.
//  Copyright (c) 2014年 wenju. All rights reserved.
//

#import "UIViewController+Category.h"

@implementation UIViewController(Category)

- (void) setTitleColor:(UIColor *)color{
    NSMutableDictionary *titleAttrDic = [NSMutableDictionary dictionary];
    [titleAttrDic setObject:[UIFont systemFontOfSize:18] forKey:UITextAttributeFont];
    [titleAttrDic setObject:[UIColor blackColor] forKey:UITextAttributeTextShadowColor];
    [titleAttrDic setObject:color forKey:UITextAttributeTextColor];
    [titleAttrDic setObject:[NSValue valueWithUIOffset:UIOffsetMake(0, 0)] forKey:UITextAttributeTextShadowOffset];
    self.navigationController.navigationBar.titleTextAttributes = titleAttrDic;
}

- (void) onNavigationBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) gotoNavigationRoot{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void) setBackBarButtonTitle:(NSString *)title{
    if (title == nil) {
        title = @"";
    }
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:nil];
}
- (void) setLeftBarButtonTitle:(NSString *)title action:(SEL)action{
    if (action == nil) {
        action = @selector(onNavigationBack);
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
}
- (void) setRightBarButtonTitle:(NSString *)title action:(SEL)action{
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
    [rightButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIFont boldSystemFontOfSize:17], NSFontAttributeName,
                                         [UIColor whiteColor], NSForegroundColorAttributeName,
                                         nil]
                               forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem =rightButton;
}

- (void) setLeftBarButtonOfDefault:(SEL)action{
    [self setLeftBarButtonImage:[UIImage imageNamed:@"nav_back"] highlightImage:nil action:action];
}

- (void) setLeftBarButtonImage:(UIImage *)image highlightImage:(UIImage *)highlightImage action:(SEL)action{
    if (action == nil) {
        action = @selector(onNavigationBack);
    }
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton setImage:highlightImage forState:UIControlStateHighlighted];
    [leftButton setFrame:CGRectMake(0, 0, 44, 44)];
    [leftButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    if (isiOS7){
        negativeSpacer.width = -16;
    }else{
        negativeSpacer.width = -5;
    }
    
    self.navigationItem.leftBarButtonItems  = @[negativeSpacer, leftItem];
}

- (void) setRightBarButtonImage:(UIImage *)image highlightImage:(UIImage *)highlightImage action:(SEL)action{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:image forState:UIControlStateNormal];
    [rightButton setImage:highlightImage forState:UIControlStateHighlighted];
    [rightButton setFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    if (isiOS7){
        negativeSpacer.width = -16;
    }else{
        negativeSpacer.width = -5;
    }
    
    self.navigationItem.rightBarButtonItems  = @[negativeSpacer, rightItem];
}

- (void) setNavigationBarWithColor:(UIColor *)color{
    CGSize size = CGSizeMake(1,1);
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self setNavigationBarWithImage:theImage];
}
-(void)setNavigationTitleView:(NSString *)title{
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0,0, 50, 44)];
    label.text=title;
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    label.backgroundColor = [UIColor clearColor];
    label.font=[UIFont boldSystemFontOfSize:17.0f];
    self.navigationItem.titleView = label ;
}
- (void) setNavigationBarWithImage:(UIImage *)image{
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    if (isiOS7) {
        self.navigationController.navigationBar.translucent = NO;
    }
}

@end
